package com.edu.homework;

public class HomeWork_PrintAB {

    public static void main(String[] args) {
        Print_A_B print_a_b = new Print_A_B();
        Runnable a = new Runnable() {
            @Override
            public void run() {
                try {
                    print_a_b.printChar();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Runnable b = new Runnable() {
            @Override
            public void run() {
                try {
                    print_a_b.printNumber();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(a).start();
        new Thread(b).start();
    }
}


class Print_A_B {
    private Object lock = new Object();
    private boolean flag = true;

    private int i = 0;

    private char c = 'A';

    public void printChar() throws InterruptedException {
        synchronized (lock) {
            while (true) {
                while (flag) {
                    System.out.println(c++);
                    Thread.sleep(1000);
                    flag = false;
                    lock.notify();
                }
                lock.wait();
            }
        }
    }

    public void printNumber() throws InterruptedException {
        synchronized (lock) {
            while (true) {
                while (!flag) {
                    System.out.println(i++);
                    Thread.sleep(1000);
                    flag = true;
                    lock.notify();
                }
                lock.wait();

            }
        }
    }

}
